
import SQLite

class DbDapp : DbBase {
    var dapp: Table = Table("dapp")
    let _id = Expression<Int64>("id")
    let _serviceId = Expression<String>("serviceId")
    let _name = Expression<String>("name")
    let _type = Expression<Int>("type")
    let _displayName = Expression<String>("displayName")
    let _portrait = Expression<String>("portrait")
    let _theme = Expression<String>("theme")
    let _url = Expression<String>("url")
    let _info = Expression<String>("info")
    let _param = Expression<String>("param")
    let _urlParam = Expression<String>("urlParam")
    let _fromUser = Expression<String>("fromUser")
    let _shareId = Expression<String>("shareId")

    func open(){
        do{
            try getDb()!.run(dapp.create(ifNotExists: true) { t in
                t.column(_id, primaryKey: .autoincrement)
                t.column(_serviceId, unique: true)
                t.column(_name)
                t.column(_type)
                t.column(_displayName)
                t.column(_portrait)
                t.column(_theme)
                t.column(_url)
                t.column(_info)
                t.column(_param)
                t.column(_urlParam)
                t.column(_fromUser)
                t.column(_shareId)
            })
            try getDb()!.run(dapp.createIndex(_serviceId, ifNotExists: true))

        }catch{
            print("\(error)")
        }
    }
    func getDappInfo(_ osnId:String) -> DMCCDapp? {
        do{
            for dapps in try getDb()!.prepare(dapp.filter(osnId == _serviceId)) {
                let u = DMCCDapp()
                u.id = dapps[_id]
                u.type = OsnDappType(rawValue: dapps[_type])
                u.serviceId = dapps[_serviceId]
                u.name = dapps[_name]
                u.displayName = dapps[_displayName]
                u.portrait = dapps[_portrait]
                u.theme = dapps[_theme]
                u.url = dapps[_url]
                u.info = dapps[_info]
                u.param = dapps[_param]
                u.urlParam = dapps[_urlParam]
                u.fromUser = dapps[_fromUser]
                u.shareId = dapps[_shareId]
                return u
            }
        }catch{
            print("\(error)")
        }
        return nil
    }
    func addDappInfo(_ info:DMCCDapp) -> Bool {
        do{
            return try getDb()!.run(dapp.upsert(
                _serviceId <- info.serviceId,
                _name <- info.name,
                _type <- info.type.rawValue,
                _displayName <- info.displayName,
                _portrait <- info.portrait,
                _theme <- info.theme,
                _url <- info.url,
                _info <- info.info,
                _param <- info.param,
                _urlParam <- info.urlParam,
                _fromUser <- info.fromUser,
                _shareId <- info.shareId,
                onConflictOf: _serviceId)) >= 0
        }catch{
            print("\(error)")
        }
        return false
    }
}
let dbDapp = DbDapp()
